﻿#include "hotelplatformserver.h"
#include "ui_hotelplatformserver.h"

HotelPlatformServer::HotelPlatformServer(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::HotelPlatformServer)
{
    ui->setupUi(this);

    ui->menuBar->hide();
    ui->statusBar->hide();
    this->setContextMenuPolicy(Qt::NoContextMenu); //去掉工具栏的右键菜单
    this->setCentralWidget(ui->widget);

    ExecSQL::searchAllReserveInfos();
    ExecSQL::searchAllUserInfos();
    ExecSQL::searchAllHotelInfos();
    ExecSQL::searchAllHouseInfos();
    ExecSQL::searchAllCommentInfos();
    ExecSQL::searchAllSaveInfos();

    m_saveImageCount = 0;
    m_msgServer = new MsgServer();
    connect(m_msgServer, SIGNAL(signalShowImageServer(QImage)),
            this, SLOT(slotShowImageServer(const QImage&)));
    connect(m_msgServer, SIGNAL(signalSaveCommentImage(const QString&,const QImage&)),
            this, SLOT(slotSaveCommentImage(const QString&,const QImage&)));
    connect(m_msgServer, SIGNAL(signalSaveHouseImage(const QString&,const QImage&)),
            this, SLOT(slotSaveHouseImage(const QString&,const QImage&)));
    connect(m_msgServer, SIGNAL(signalSaveHotelImage(const QString&,const QImage&)),
            this, SLOT(slotSaveHotelImage(const QString&,const QImage&)));
    connect(m_msgServer, SIGNAL(signalUploadPicSuccessed()),
            this, SLOT(slotUploadPicSuccessed()));

    /// 保存用户头像 + 用户实名认证图片
    connect(m_msgServer, SIGNAL(signalSaveUserCertImage(const QString&,const QImage&)),
            this, SLOT(slotSaveUserCertImage(const QString&,const QImage&)));
    connect(m_msgServer, SIGNAL(signalSaveUserHeadImage(const QString&,const QImage&)),
            this, SLOT(slotSaveUserHeadImage(const QString&,const QImage&)));

    m_userManage = new UserManage(ui->widget);
    m_hotelManage = new HotelManage(ui->widget);
    m_hotelManage->close();
    m_pictureShow = new PictureShow(this);
    m_pictureShow->show();
}

HotelPlatformServer::~HotelPlatformServer()
{
    delete ui;
}

void HotelPlatformServer::slotShowImageServer(const QImage& image)
{
    qDebug() << "show image " << image.width() << " " << image.height();
    m_pictureShow->showPic(image);
}

void HotelPlatformServer::on_actionUserManage_triggered()
{
    m_userManage->show();
    m_hotelManage->close();
}
void HotelPlatformServer::on_actionHotelManage_triggered()
{
    m_hotelManage->show();
    m_userManage->close();
}

void HotelPlatformServer::slotUploadPicSuccessed()
{
    m_currentCommentID.clear();
    m_currentHouseID.clear();
    m_currentHotelID.clear();
    m_currentUserID.clear();
}
void HotelPlatformServer::slotSaveCommentImage(const QString& commentID, const QImage& image)
{
    if(image.width() > 0 && image.height() > 0)
    {
        qDebug() << "here" << m_currentCommentID << commentID << m_saveImageCount;
        if (m_currentCommentID != commentID)
        {
            m_saveImageCount = 1;
            m_currentCommentID = commentID;
        }
        else
        {
            m_saveImageCount++;
        }
        QString path = GlobalVars::g_localConfig.readCommentImagePath();
        QString imageName = QString("%1_%2.jpg").arg(commentID).arg(m_saveImageCount);
        image.save(path + "/" + imageName);

        for (int i = 0; i < m_saveImageCount; i++)
        {
            path += ("#" + QString("%1_%2.jpg").arg(commentID).arg(i+1));
         }
        ExecSQL::modifyCommentInfoForPhoto(commentID,path);
    }
}
void HotelPlatformServer::slotSaveHouseImage(const QString& houseID, const QImage& image)
{
    qDebug() << "here" << m_currentHouseID << houseID << m_saveImageCount;
    if(image.width() > 0 && image.height() > 0)
    {
        if (m_currentHouseID != houseID)
        {
            m_saveImageCount = 1;
            m_currentHouseID = houseID;
        }
        else
        {
            m_saveImageCount++;
        }
        QString path = GlobalVars::g_localConfig.readHouseImagePath();
        QString imageName = QString("%1_%2.jpg").arg(houseID).arg(m_saveImageCount);
        image.save(path + "/" + imageName);

        for (int i = 0; i < m_saveImageCount; i++)
        {
            path += ("#" + QString("%1_%2.jpg").arg(houseID).arg(i+1));
         }
        ExecSQL::modifyHouseInfoForPhoto(houseID,path);
    }
}
void HotelPlatformServer::slotSaveHotelImage(const QString& hotelID, const QImage& image)
{
    qDebug() << "here" << m_currentHotelID << hotelID << m_saveImageCount;
    if (m_currentHotelID != hotelID)
    {
        m_saveImageCount = 1;
        m_currentHotelID = hotelID;
    }

    if (m_saveImageCount == 1)
    {
        m_saveImageCount++;
        if(image.width() > 0 && image.height() > 0)
        {
            QString path = GlobalVars::g_localConfig.readLicenseImagePath();
            QString imageName = QString("%1License.jpg").arg(hotelID);
            QString imagePath = path + "/" + imageName;
            image.save(imagePath);

            ExecSQL::modifyHotelInfoForLicense(hotelID,imagePath);
        }
    }
    else
    {
        if(image.width() > 0 && image.height() > 0)
        {
            QString path = GlobalVars::g_localConfig.readHotelImagePath();
            QString imageName = QString("%1_%2.jpg").arg(hotelID).arg(m_saveImageCount-1);
            image.save(path + "/" + imageName);

            for (int i = 1; i < m_saveImageCount; i++)
            {
                path += ("#" + QString("%1_%2.jpg").arg(hotelID).arg(i));
             }
            qDebug() << imageName << path;
            ExecSQL::modifyHotelInfoForPhoto(hotelID,path);
            m_saveImageCount++;
        }
    }

}
void HotelPlatformServer::slotSaveUserCertImage(const QString& userID, const QImage& image)
{
    qDebug() << "here" << m_currentUserID << userID << m_saveImageCount;
    if(image.width() > 0 && image.height() > 0)
    {
        if (m_currentUserID != userID)
        {
            m_saveImageCount = 1;
            m_currentUserID = userID;
        }
        else
        {
            m_saveImageCount++;
        }

        QString path = GlobalVars::g_localConfig.readCertImagePath();
        QString imageName = QString("%1_%2.jpg").arg(userID).arg(m_saveImageCount-1);
        image.save(path + "/" + imageName);

        for (int i = 0; i < m_saveImageCount; i++)
        {
            path += ("#" + QString("%1_%2.jpg").arg(userID).arg(i));
         }
        qDebug() << imageName << path;
        ExecSQL::modifyUserInfoForCertPath(userID,path);
    }
}
void HotelPlatformServer::slotSaveUserHeadImage(const QString& userID, const QImage& image)
{
    qDebug() << "here" << m_currentUserID << userID << m_saveImageCount;
    if(image.width() > 0 && image.height() > 0)
    {
        if (m_currentUserID != userID)
        {
            m_currentUserID = userID;
        }

        QString path = GlobalVars::g_localConfig.readHeadImagePath();
        QString imageName = QString("%1Head.jpg").arg(userID);
        QString imagePath = path + "/" + imageName;
        image.save(imagePath);

        ExecSQL::modifyUserInfoForHeadPic(userID,imagePath);
    }
}
